//
//  UIImage+Compress.m
//  FlagForiOS
//
//  Created by Steven.Yung on 2017/1/3.
//  Copyright © 2017年 Minidai. All rights reserved.
//

#import "UIImage+Compress.h"

@implementation UIImage (Compress)

- (UIImage *)compressImageWithSize: (CGSize)size {
    
    UIGraphicsBeginImageContext(size);
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return resizedImage;
}

- (UIImage *)compressionRatioImageWithSize:(CGSize)size {
    
    UIImage *compressedImage;
    if (nil == self) {
        
        compressedImage = nil;
        
    } else {
        compressedImage = self;
        CGSize oldsize = self.size;
        //计算宽高比
        CGFloat aspectRatio = oldsize.width / oldsize.height;
        
        CGRect rect;
        if (size.width / size.height > oldsize.width / oldsize.height) {
            
            size.width = size.height * aspectRatio; //重新调整高度，避免重新绘制后产生多于的部分空白
            rect.size.width = size.height * oldsize.width / oldsize.height;
            rect.size.height = size.height;
            rect.origin.x = (size.width - rect.size.width) / 2;
            rect.origin.y = 0;
            
        } else {
            size.height = size.width * aspectRatio; //重新调整宽度，避免重新绘制后产生多于的部分空白
            rect.size.width = size.width;
            rect.size.height = size.width * oldsize.height / oldsize.width;
            rect.origin.x = 0;
            rect.origin.y = (size.height - rect.size.height) / 2;
        }
        UIGraphicsBeginImageContext(size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        //clear background
        CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
        UIRectFill(CGRectMake(0, 0, size.width, size.height));
        [self drawInRect:rect];
        compressedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    
    return compressedImage;
}

- (UIImage *)compressImageWithScalingAndCroppingForSize: (CGSize)targetSize {
    
    UIImage *sourceImage = self;//[self compressionRatioImageWithSize:targetSize];
    
    UIImage *compressedImage = nil;
    //图片原尺寸
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
    
    if (CGSizeEqualToSize(imageSize, targetSize) == NO)
    {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        if (widthFactor > heightFactor) {
            
            scaleFactor = widthFactor; // scale to fit height
        } else {
            scaleFactor = heightFactor; // scale to fit width
        }
        scaledWidth= width * scaleFactor;
        scaledHeight = height * scaleFactor;
        // center the image
        if (widthFactor > heightFactor) {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        } else if (widthFactor < heightFactor) {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }
    
    UIGraphicsBeginImageContext(targetSize); // this will crop
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width= scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    [sourceImage drawInRect:thumbnailRect];
    compressedImage = UIGraphicsGetImageFromCurrentImageContext();
    
    if(compressedImage == nil)
    NSLog(@"could not scale image");
    //pop the context to get back to the default
    UIGraphicsEndImageContext();
    
    return compressedImage;
}

@end
